package com.example.home.findWork.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.findWork.bean.WordDetaiBean;
import com.example.home.findWork.bean.WorkExperience;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

public class WordDetailActivity extends AppCompatActivity {
    private ImageView back;
    private TextView professionId;
    private TextView salary;
    private TextView address;
    private TextView need;
    private TextView contacts;
    private TextView obligation;
    private TextView companyName;
    private Button btnSend;
    private WordDetaiBean wordDetaiBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_detail);
        Intent intent = getIntent();
        String wordId = intent.getStringExtra("wordId");
        initView();
        getData(wordId);


    }

    public interface WorkExperienceCallback {
        void onWorkExperienceLoaded(boolean hasWorkExperience);
        void onWorkExperienceLoadFailed(String errorMessage);
    }


    private void getData(String wordId) {
        OKHttpTool.doNetRequest("prod-api/api/job/post/"+wordId,
                OKHttpTool.GET,
                WordDetaiBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        wordDetaiBean = (WordDetaiBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                professionId.setText(wordDetaiBean.getData().getName());
                                salary.setText(wordDetaiBean.getData().getSalary()+"元");
                                address.setText("位置："+ wordDetaiBean.getData().getAddress());
                                need.setText("岗位需求："+ wordDetaiBean.getData().getNeed());
                                contacts.setText("联系人："+ wordDetaiBean.getData().getContacts());
                                obligation.setText("岗位职责："+ wordDetaiBean.getData().getObligation());
                                companyName.setText("公司："+String.valueOf(wordDetaiBean.getData().getCompanyName()));
                            }
                        });
                    }


                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void initView() {

        back = findViewById(R.id.back);
        professionId = findViewById(R.id.professionId);
        salary = findViewById(R.id.salary);
        address = findViewById(R.id.address);
        need = findViewById(R.id.need);
        contacts = findViewById(R.id.contacts);
        obligation = findViewById(R.id.obligation);
        companyName = findViewById(R.id.companyName);
        btnSend = findViewById(R.id.btn_send);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在需要调用的地方
                checkWorkExperience(new WorkExperienceCallback() {
                    @Override
                    public void onWorkExperienceLoaded(boolean hasWorkExperience) {
                        // 处理获取到的工作经历信息
                        if (hasWorkExperience) {
                            // 有工作经历
                            applyForJob();
                        } else {
                            // 如果缺少工作经历或教育经历，发送广播通知 MainActivity 跳转到 OnlyMeFragment
                            Intent intent = new Intent("bov_action4");
                            intent.putExtra("fragmentToLoad", "OnlyMeFragment");
                            sendBroadcast(intent);
                            WordDetailActivity.this.finish();
                            Toast.makeText(WordDetailActivity.this,"完善简历后再进行投递",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onWorkExperienceLoadFailed(String errorMessage) {
                        // 处理加载工作经历失败的情况
                    }
                });
            }
        });


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WordDetailActivity.this.finish();
            }
        });
    }

    private void checkWorkExperience(WorkExperienceCallback callback) {
        OKHttpTool.doNetRequest("prod-api/api/job/resume/queryResumeByUserId/1",
                OKHttpTool.GET,
                WorkExperience.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        WorkExperience workExperience = (WorkExperience) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                boolean hasWorkExperience = workExperience.getData() != null &&
                                        !workExperience.getData().getEducation().isEmpty();
                                callback.onWorkExperienceLoaded(hasWorkExperience);

                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                        callback.onWorkExperienceLoadFailed(msg);
                    }
                });
    }

    private void applyForJob() {
        JSONObject jsonObject = new JSONObject();
        Date date = new Date();
        // 创建 SimpleDateFormat 对象并指定日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 使用 SimpleDateFormat 格式化日期
        String formattedDate = dateFormat.format(date);
        try {
            jsonObject.put("companyId",wordDetaiBean.getData().getCompanyId());
            jsonObject.put("satrTime",formattedDate);
            jsonObject.put("postId",wordDetaiBean.getData().getProfessionId());
            jsonObject.put("postName",wordDetaiBean.getData().getName());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKHttpTool.doNetRequest("prod-api/api/job/deliver",
                OKHttpTool.POST,
                GeneralClass.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        GeneralClass generalClass = (GeneralClass) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (generalClass.getCode() == 200) {
                                    Toast.makeText(WordDetailActivity.this,"投递成功",Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(WordDetailActivity.this,"投递失败",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                    }
                });
    }

}